package com.definity.toolkit.data;

import java.io.ByteArrayOutputStream;
import java.io.InputStream;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import javax.xml.bind.JAXBContext;
import javax.xml.bind.JAXBException;
import javax.xml.bind.Marshaller;
import javax.xml.bind.Unmarshaller;
import javax.xml.bind.annotation.XmlAttribute;
import javax.xml.bind.annotation.XmlElement;
import javax.xml.bind.annotation.XmlRootElement;

@XmlRootElement
public class Feed {

	private String id;
	private String title;
	private String link;
	private String description;
	private String language;
	private Date publishedDate;
	private Date updateDate;
	private Integer ttl;
	private String copyright;
	private Image image;
	private List<Category> category = new ArrayList<Category>();
	private List<Author> author = new ArrayList<Author>();
	private List<Entry> entry = new ArrayList<Entry>();
	
	public Feed() {

	}

	@XmlAttribute
	public String getId() {
		return id;
	}

	public void setId(String id) {
		this.id = id;
	}

	@XmlElement
	public String getTitle() {
		return title;
	}

	public void setTitle(String title) {
		this.title = title;
	}

	@XmlElement
	public String getLink() {
		return link;
	}
	
	public void setLink(String link) {
		this.link = link;
	}
	
	@XmlElement
	public String getDescription() {
		return description;
	}

	public void setDescription(String subtitle) {
		this.description = subtitle;
	}

	@XmlElement
	public String getCopyright() {
		return copyright;
	}

	public void setCopyright(String copyright) {
		this.copyright = copyright;
	}

	@XmlElement
	public Date getUpdateDate() {
		return updateDate;
	}

	public void setUpdateDate(Date updateDate) {
		this.updateDate = updateDate;
	}

	@XmlElement
	public Image getImage() {
		return image;
	}

	public void setImage(Image logo) {
		this.image = logo;
	}

	@XmlElement
	public Integer getTtl() {
		return ttl;
	}
	
	public void setTtl(Integer ttl) {
		this.ttl = ttl;
	}
	
	@XmlElement
	public String getLanguage() {
		return language;
	}
	
	public void setLanguage(String language) {
		this.language = language;
	}
	
	@XmlElement
	public Date getPublishedDate() {
		return publishedDate;
	}
	
	public void setPublishedDate(Date publishedDate) {
		this.publishedDate = publishedDate;
	}

	@XmlElement
	public List<Author> getAuthor() {
		return author;
	}

	public void setAuthor(List<Author> authors) {
		this.author = authors;
	}

	@XmlElement
	public List<Entry> getEntry() {
		return entry;
	}

	public void setEntry(List<Entry> entries) {
		this.entry = entries;
	}
	
	public void add(Author author) {
		this.author.add(author);
	}
	
	public void remove(Author author) {
		this.author.remove(author);
	}
	
	public void add(Entry entry) {
		this.entry.add(entry);
	}

	public void remove(Entry entry) {
		this.entry.remove(entry);
	}
	
	public List<Category> getCategory() {
		return category;
	}
	
	public void setCategory(List<Category> category) {
		this.category = category;
	}
	
	public void add(Category category) {
		this.category.add(category);
	}

	public void remove(Category category) {
		this.category.remove(category);
	}
	
	public String xml() throws DataException {
		ByteArrayOutputStream bos = new ByteArrayOutputStream();
		
		try {
			JAXBContext jc = JAXBContext.newInstance(Feed.class, Entry.class, Author.class);
			Marshaller m = jc.createMarshaller();
			m.marshal(this, bos);
		} catch (JAXBException e) {
			throw new DataException(e.getMessage(), e);
		}
		return new String(bos.toByteArray());
	}
	
	public static Feed parse(InputStream is) throws DataException {
		try {
			JAXBContext jc = JAXBContext.newInstance(Feed.class, Entry.class, Author.class);
			Unmarshaller um = jc.createUnmarshaller();
			return (Feed) um.unmarshal(is);
		} catch (JAXBException e) {
			throw new DataException(e.getMessage(), e);
		}
	}
	
}
